﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.Data.OleDb;
using OgilvyOne.DALCore;
namespace OgilvyOne.BLL.BLLEngine
{
    /// <summary>
    /// 封装后的通用查询操作
    /// </summary>
    public static class CommonQuery
    {
        /// <summary>
        /// 执行Sql语句返回首行首列数据
        /// 如果执行过程中有异常,则抛出;
        /// 如果查询结果为空,则对象为空
        /// </summary>
        /// <param name="queryString">要执行的Sql语句列表</param>
        /// <returns>某数据类型</returns>
        public static object GetScalar(string connString, string sql)
        {
            DBService service = DataService.GetDBService(connString);
            return service.GetScalar(sql);
        }
        /// <summary>
        /// 执行Sql语句返回数据记录
        /// 如果执行过程中有异常,则抛出;
        /// 如果查询结果为空,则OleDbDataReader为空
        /// </summary>
        /// <param name="queryString">要执行的Sql语句列表</param>
        /// <returns>返回记录</returns>
        public static OleDbDataReader GetDataReader(string connString, string sql)
        {
           
            DBService service = DataService.GetDBService(connString);
            return service.GetDataReader(sql);
        }
        /// <summary>
        /// 执行单条Sql获取数据表
        /// 如果执行过程出现异常,则抛出
        /// 如果查询结果为空,则DataTable为初始化过的空表
        /// </summary>
        /// <param name="connString">连接字符串</param>
        /// <param name="sql">要执行的单条Sql</param>
        /// <returns>
        /// 执行结果,以DataTable的形式返回</returns>
        public static DataTable GetDataBySql(string connString, string sql)
        {
           
            DBService service = DataService.GetDBService(connString);
            return GetDataTable(service as ICommonQueryService, sql);
        }
        /// <summary>
        /// 执行一组Sql获取数据集
        /// 如果执行结果出现异常,则抛出
        /// 如果查询结果为空,则DataSet为初始化过的空数据集
        /// </summary>
        /// <param name="connString">连接字符串</param>
        /// <param name="sqlList">要执行的一组Sql</param>
        /// <returns>
        /// 执行结果,各条Sql依次执行,并以DataSet的形式返回</returns>
        public static DataSet GetDataBySqlList(string connString, string[] sqlList)
        {
           
            DBService service = DataService.GetDBService(connString);
            return GetDataSet(service as ICommonQueryService, sqlList);
        }
        
        /// <summary>
        /// 执行单条无返回值的Sql
        /// 如果执行结果出现异常,则抛出
        /// </summary>
        /// <param name="connString">连接字符串</param>
        /// <param name="sql">要执行的单条Sql</param>
        public static int ExecuteNonQueryBySql(string connString, string sql)
        {
            DBService service = DataService.GetDBService(connString);
            return ExecuteNonQuery(service as ICommonQueryService, sql);
        }
        /// <summary>
        /// 执行一组无返回值的Sql
        /// 如果执行结果出现异常,则中断整个执行且抛出
        /// </summary>
        /// <param name="connString">连接字符串</param>
        /// <param name="sqlList">要执行的一组Sql</param>
        public static void ExecuteNonQueryBySqlList(string connString, string[] sqlList)
        {

            DBService service = DataService.GetDBService(connString);
            ExecuteNonQuery(service as ICommonQueryService, sqlList);
        }
        
        /// <summary>
        /// 执行无数据集或数据表返回的存储过程
        /// </summary>
        /// <param name="connString">连接字符串</param>
        /// <param name="procName">存储过程名称</param>
        /// <param name="parms">存储过程参数</param>
        /// <returns>影响行数</returns>
        public static int ExecuteStoredProcedure(string connString, string procName, OleDbParameter[] parms)
        {
            DBService service = DataService.GetDBService(connString);
            return service.ExecuteStoredProcedure(procName, parms);
        }
        /// <summary>
        /// 执行有数据集或数据表返回的DB的存储过程
        /// </summary>
        /// <param name="connString">连接字符串</param>
        /// <param name="procName">存储过程名称</param>
        /// <param name="parms">存储过程参数</param>
        /// <returns>执行结果,以DataTable的形式返回</returns>
        public static DataTable ExecuteStoredProcedureDataTable(string connString, string procName, OleDbParameter[] parms)
        {
            DBService service = DataService.GetDBService(connString);
            DataSet ds = service.ExecuteStoredProcedureDataSet(procName, parms);
            DataTable table = new DataTable();
            if (ds.Tables.Count > 0)
                table = ds.Tables[0];
            return table;
        }

        /// <summary>
        /// 通用接口执行单条Query,返回数据表
        /// </summary>
        /// <param name="service">通用接口</param>
        /// <param name="query">要执行的Query语句</param>
        /// <returns>执行结果</returns>
        static DataTable GetDataTable(ICommonQueryService service, string query)
        {
            DataTable table = new DataTable();
            if (service != null)
            {
                DataSet ds = service.GetDataSet(query);
                if (ds.Tables.Count > 0)
                    table = ds.Tables[0];
            }
            return table;
        }
        /// <summary>
        /// 通用接口执行多条Query,返回数据集
        /// </summary>
        /// <param name="service">通用接口</param>
        /// <param name="queryList">要执行的多条Query</param>
        /// <returns>执行结果</returns>
        static DataSet GetDataSet(ICommonQueryService service, string[] queryList)
        {
            DataSet ds = new DataSet();
            if (service != null)
                ds = service.GetDataSet(queryList);
            return ds;
        }
        /// <summary>
        /// 通用接口执行无返回的单条Query
        /// </summary>
        /// <param name="service">通用接口</param>
        /// <param name="query">要执行的单条Query</param>
        static int ExecuteNonQuery(ICommonQueryService service, string query)
        {
            if (service != null)
            {
                return service.ExecuteNonQuery(query);
            }
            else
            {
                throw new Exception("执行sql语句出错！");
            }
        }
        /// <summary>
        /// 通用接口执行无返回的多条Query
        /// </summary>
        /// <param name="service">通用接口</param>
        /// <param name="queryList">要执行的多条Query</param>
        static void ExecuteNonQuery(ICommonQueryService service, string[] queryList)
        {
            if (service != null)
                service.ExecuteNonQuery(queryList);
        }


      

    }
}
